package kr.co.creativeideas.gooq.activity;

import java.util.List;
import java.util.regex.Pattern;

import kr.co.creativeideas.gooq.common.ServerRequest;
import kr.co.creativeideas.gooq.common.Tools;
import kr.co.creativeideas.gooq.entity.DataReceive;
import kr.co.creativeideas.gooq.entity.DataSend;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ResultActivity extends Activity implements View.OnClickListener {

	private List<DataReceive> recordList;
	private TextView mScore;
	private TextView mResultScore;
	private EditText mInputID;
	private Button mSummit;
	private Button mRetry;
	private Button mExit;
	private DataSend mDateSend;
	private ServerRequest mServerRequest;
	private String sNickname;
	private String sID;
	private String sResultScore;
	private Intent mIntent;
	private View mTile;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resultactivity);
		
		mTile = (ImageView)findViewById(R.id.tilebackgroundview);		
		BitmapDrawable bgImage = (BitmapDrawable)getResources().getDrawable(R.drawable.tiled);
		bgImage.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		mTile.setBackgroundDrawable(bgImage);
		
		mIntent = getIntent();
		
		mScore = (TextView) findViewById(R.id.score);
		mScore.setTypeface(Tools.getTypeface(this, Tools.FONT_DIN_BLACKALTERNATE));
		mResultScore = (TextView) findViewById(R.id.result_score);
		mResultScore.setTypeface(Tools.getTypeface(this, Tools.FONT_DIN));
		mInputID = (EditText) findViewById(R.id.inputID);
		mSummit = (Button) findViewById(R.id.result_submit);
		mRetry = (Button) findViewById(R.id.result_retry);
		mExit = (Button) findViewById(R.id.result_exit);

		mResultScore.setText(mIntent.getStringExtra("score"));
		mInputID.setHint("Your ID");		
		
		mSummit.setOnClickListener(this);
		mRetry.setOnClickListener(this);
		mExit.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.result_submit:			
			onSubmit();			
			break;
		case R.id.result_retry:
			mIntent = new Intent(this, MainActivity.class);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(mIntent);
			break;
		case R.id.result_exit:
			Tools.ExitDialog(this, getString(R.string.exit), getString(R.string.message_exit));
			break;		
		}
	}

	private void onSubmit() {
		sID = Tools.getDevieID(this);
		Editable mEditable = mInputID.getText();
		sNickname = mEditable.toString();
		sResultScore = (String) mResultScore.getText();

		if (checkFormValid(sNickname)) {
			mDateSend = new DataSend(sID, sNickname, sResultScore);
			mServerRequest = new ServerRequest(this,getString(R.string.httpurl),mDateSend);			
			getResult();			
		} else {
			Tools.MessageBox(this, getString(R.string.message_id));
		}
	}

	private void getResult() {
		if (mServerRequest.getResult() != null) {
			Intent intent = new Intent(this, RankActivity.class);
			intent.putExtra("uRankRecord", mServerRequest.getResult());
			intent.putExtra("uScore", sResultScore);
			intent.putExtra("uNickname", sNickname);
			startActivity(intent);
			finish();
		} else {
			Intent intent = new Intent(this, RankActivity.class);
			intent.putExtra("uRankRecord", "Sorry");
			startActivity(intent);
			finish();
		}
	}

	private boolean checkFormValid(String str) {
		boolean result = Pattern.matches("[\\uAC00-\\uD7A3a-zA-Z0-9]{1,10}",str);
		if (str.length() > 0 && result) {
			return true;
		}
		return false;
	}
}
